<?php
namespace app\admin\controller;
use think\Controller;

Class Conf extends BaseAdmin
{

    public function Conflist(){

        $conf=db('tp_conf');
        if(request()->isPost()){
            $data=input('post.');

            //复选框为空值
            $CheckFields2d=$conf->field('ename')->where(array('form_type'=>'checkbox'))->select();
            $CheckFields=array();
            if($CheckFields2d){
                foreach ($CheckFields2d as $k => $v) {
                    $CheckFields[]=$v['ename'];
                }
            }
            $Allfields=array();
            //处理文字数据
            foreach ($data as $k => $v) {
                $Allfields[]=$k;
                if(is_array($v)){
                    $value=implode(',',$v);
                    $conf->where(array('ename'=>$k))->update(['value'=>$value]);

                }else{
                    $conf->where(array('ename'=>$k))->update(['value'=>$v]);
                }
            }

            //如果复选框没有选择一个，则改为空值
            foreach ($CheckFields as $k => $v) {
                if(!in_array($v, $Allfields)){
                    $conf->where(array('ename'=>$v))->update(['value'=>'']);
                }
            }



            //处理文件
            if($_FILES){
                foreach ($_FILES as $k=> $v) {
                    if($v['tmp_name']){
                        $imgs=$conf->field('value')->where(array('ename'=>$k))->find();
                        if($imgs['value']){
                            $img=IMG_UPLOADS.$imgs['value'];
                            if(file_exists($img)){
                                @unlink($img);
                            }
                        }
                        $imgSrc=$this->upload($k);
                        $conf->where(array('ename'=>$k))->update(['value'=>$imgSrc]);
                    }
                }
            }

            // dump($data);
            // die;
        }
        
        $ShopConfRes=$conf->where(array('conf_type'=>1))->order('sort desc')->select();
        $GoodsConfRes=$conf->where(array('conf_type'=>2))->order('sort desc')->select();
        $this->assign(
            ['ShopConfRes'=>$ShopConfRes,
            'GoodsConfRes'=>$GoodsConfRes
            ]
                    );
        return $this->fetch();
    }

    //配置列表
    public function lst()
    {
        $conf=db('tp_conf');
        if(request()->isPost()){
            $data=input("post.");
            foreach ($data['sort'] as $k => $v) {
           $conf->where(array('id'=>$k))->update(['sort'=>$v]); 
        }
        $this->success('排序成功', 'conf/lst');

        }
        
        $confRes=$conf->order('sort DESC')->paginate(10);
        $this->assign('confRes',$confRes);
        // dump($data->render());
        // exit;
        return $this->fetch('list');
    }

    //配置添加
    public function add()
    {
         
        if(request()->isPost()){
            $data=input('post.');

            //将中文逗号“，”改为英文逗号“,”
            if($data["form_type"]=="redio" ||$data["form_type"]=="select" ||$data["form_type"]=="checkbox" ){
                $data['values']=str_replace('，',',',$data['values']);
                $data['value']=str_replace('，',',',$data['value']);
            }
            
            //验证
            // $validate = new \app\admin\validate\conf;
            // if (!$validate->check($data)) {
            //     $this->error($validate->getError());
            // }
            
            $add=db('tp_conf')->insert($data);
            if($add){
                $this->success('保存成功','lst');
            }
            else{
                $this->error('保存失败');
            }

        }
        return $this->fetch();

    }

    //配置编辑
    public function edit($id)
    {
        $confRes=db('tp_conf')->where(array('id'=>$id))->find();
        $this->assign('confRes',$confRes);
        if(request()->isPost()){
            $data=input('post.');

             //将中文逗号“，”改为英文逗号“,”
            if($data["form_type"]=="redio" ||$data["form_type"]=="select" ||$data["form_type"]=="checkbox" ){
                $data['values']=str_replace('，',',',$data['values']);
                $data['value']=str_replace('，',',',$data['value']);
            }
            
            $up=db('tp_conf')->where(array('id'=>$id))->update($data);
            // dump($up);
            // exit;
            if($up!==false){
                $this->success('保存成功','lst');
            }
            else{
                $this->error('保存失败');
            }

        }
        return $this->fetch();
    }

    //配置删除
    public function del($id)
    {
        $del=db('tp_conf')->delete($id);
        if($del){
            exit(json_encode(array('code'=>1,'msg'=>'删除成功')));
           

        }else{
           exit(json_encode(array('code'=>2,'msg'=>'删除失败')));
        }
        
        
    }



    //图片上传
    public function upload($imgName){
        $file=request()->file($imgName);
        if($file==null){
            exit(json_encode(array('msg'=>'没有文件上传')));
        }
        $info=$file->move('../public/static/uploads');
        $ext=$info->getExtension();//获取后缀名
        if(!in_array($ext,array('jpg','png','gif','jpeg'))){
            exit(json_encode(array('msg'=>'文件格式不符合')));
        }
        if($info){
            return $img=$info->getSaveName();

        }else{
            $file->getError();
            die;
        }
        
        
    }

    
    


}